home *** CD-ROM | disk | FTP | other *** search
/ Magnum One / Magnum One (Mid-American Digital) (Disc Manufacturing).iso / d12 / tcoop.arc / TCOOP2.ARC / BXSTRTCH.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  1991-10-26  |  4.3 KB  |  165 lines

  1. // bxstrtch.cpp: Demonstrates scrollable windows in graphics mode. 
  2. // A scrollable window will appear which will allow you to inter-
  3. // actively alter the size of a box by using the scroll bars. 
  4. // Alternatively, you can use the arrow, and shift-arrow keys 
  5. // to change the size of the box. To quit the program use ALT-X. 
  6.  
  7. #include "math.h"
  8. #include "gscroll.h"
  9.  
  10. class BoxStretcher : public Swso {
  11. public:
  12.   int BoxWd, BoxHt, MaxWd, MaxHt;
  13.   BoxStretcher(int Ba, int Fa, ColorPak &Cp);
  14.   virtual void SetSize(int W, int H);
  15.   virtual void Draw(void);
  16.   virtual void Redraw(void);
  17.   virtual void SendScrollPosn(void);
  18.   virtual void RcvScrollPosn(float P, BarOrient Which);
  19.   virtual void OnKeyStroke(MsgPkt &M);
  20. };
  21.  
  22. BoxStretcher::BoxStretcher(int Ba, int Fa, ColorPak &Cp)
  23. // Initializes the box stretcher object 
  24. : Swso(Ba, Fa, Cp)
  25. {
  26.   BoxWd = 0; BoxHt = 0;
  27. }
  28.  
  29. void BoxStretcher::SetSize(int W, int H)
  30. // Sets the size of the window and the start and maximum dimensions
  31. // of the box 
  32. {
  33.   Swso::SetSize(W, H);
  34.   MaxWd = Window->Panel->Interior->Wd; 
  35.   MaxHt = Window->Panel->Interior->Ht;
  36.   if (BoxWd > MaxWd) BoxWd = MaxWd; 
  37.   if (BoxHt > MaxHt) BoxHt = MaxHt; 
  38. }
  39.  
  40. void BoxStretcher::Draw(void)
  41. // Draws a rectangle centered in the interior of Panel 
  42. {
  43.   Swso::Draw();
  44.   Mouse.Hide();
  45.   setcolor(3);
  46.   setwritemode(XOR_PUT);
  47.   Rso *Wi = Window->Panel->Interior;
  48.     rectangle(Wi->Xul+Wi->Wd / 2 - BoxWd / 2,
  49.               Wi->Yul+Wi->Ht / 2 - BoxHt / 2, 
  50.               Wi->Xul+Wi->Wd / 2 + BoxWd / 2,
  51.               Wi->Yul+Wi->Ht / 2 + BoxHt / 2);
  52.   setwritemode(COPY_PUT);
  53.   Mouse.Show();
  54. }
  55.  
  56. void BoxStretcher::Redraw(void)
  57. // Due to the inherited Redraw method drawing the box
  58. // twice, and because it's drawn via XOR, we need to 
  59. // draw the box again to make it visible. That's the
  60. // reason for the call to Draw below. 
  61. {
  62.   Swso::Redraw();
  63.   Draw();
  64. }
  65.  
  66. void BoxStretcher::SendScrollPosn(void)
  67. // Sends message to slide bars to update their positions relative
  68. // to the size of the box 
  69. {
  70.   HzSlide->RcvScrollPosn(float(BoxWd)/float(MaxWd),HzOrient);
  71.   VtSlide->RcvScrollPosn(float(BoxHt)/float(MaxHt),VtOrient);
  72. }
  73.  
  74. void BoxStretcher::RcvScrollPosn(float P, BarOrient Which)
  75. // Based on the value of P, which should be between 0 and 1, 
  76. // set the size of the box 
  77. {
  78.   Draw();   // Erase old position of box 
  79.   if (Which == HzOrient)
  80.      BoxWd = ceil(P * MaxWd);
  81.      else BoxHt = ceil(P * MaxHt);
  82.   Draw();   // Draw new box 
  83. }
  84.  
  85. void BoxStretcher::OnKeyStroke(MsgPkt &M)
  86. // Resizes the box according to the keys pressed  
  87. {
  88.   switch(M.Code) {
  89.     case UpKey:
  90.       Draw(); 
  91.       if (BoxHt > 0) BoxHt--;
  92.       Draw();
  93.       SendScrollPosn();   // Update bar position 
  94.     break;
  95.     case DownKey:
  96.       Draw(); 
  97.       if (BoxHt < MaxHt) BoxHt++;
  98.       Draw();
  99.       SendScrollPosn();
  100.     break;
  101.     case LeftKey:
  102.       Draw(); 
  103.       if (BoxWd > 0) BoxWd--;
  104.       Draw();
  105.       SendScrollPosn();
  106.     break;
  107.     case RightKey:
  108.       Draw(); 
  109.       if (BoxWd < MaxWd) BoxWd++;
  110.       Draw(); 
  111.       SendScrollPosn();
  112.     break;
  113.     case ShiftRight:
  114.       Draw(); 
  115.       if ((BoxWd+MaxWd / 8) < MaxWd) 
  116.          BoxWd += MaxWd / 8; else BoxWd = MaxWd;
  117.       Draw(); 
  118.       SendScrollPosn();
  119.     break;
  120.     case ShiftLeft:
  121.       Draw(); 
  122.       if (BoxWd > (MaxWd / 8))
  123.          BoxWd -= MaxWd / 8; else BoxWd = 0;
  124.       Draw(); 
  125.       SendScrollPosn();
  126.     break;
  127.     case ShiftDnKey: 
  128.       Draw(); 
  129.       if ((BoxHt+MaxHt / 8) < MaxHt)
  130.          BoxHt += MaxHt / 8; else BoxHt = MaxHt;
  131.       Draw(); 
  132.       SendScrollPosn();
  133.     break;
  134.     ShiftUpKey:
  135.       Draw(); 
  136.       if (BoxHt > (MaxHt / 8))
  137.         BoxHt -= MaxHt / 8; else BoxHt = 0;
  138.       Draw(); 
  139.       SendScrollPosn();
  140.     break;
  141.     default: Swso::OnKeyStroke(M);
  142.   }
  143. }
  144.  
  145. BoxStretcher *Bs;
  146. MsgPkt M;
  147. int Gd, Gm;
  148. main()
  149. {
  150.   Gd = DETECT;
  151.   Setup(MouseOptional, Gd, Gm, "\\tcpp\\bgi", SANS_SERIF_FONT);
  152.   FullScrn->Panel->Clear(' ', 0x70);
  153.   Bs = new BoxStretcher(Relief + 3, Closeable, GrphColors);
  154.   Bs->SetSize(301, 151);
  155.   Bs->Open(FullScrn, 75, 75);
  156.   // Set size of box to quarter of window size 
  157.   Bs->BoxWd = Bs->Window->Panel->Interior->Wd / 2;
  158.   Bs->BoxHt = Bs->Window->Panel->Interior->Ht / 2;
  159.   Bs->Draw();
  160.   Bs->SendScrollPosn();
  161.   MainEventLoop();
  162.   CleanUp();
  163. }
  164.  
  165.